﻿using System;

namespace ParsingProject {

    /// <summary>
    /// Tokenizer to split a string into an array of strings.
    /// Calling code can retrieve and peek at next tokens.
    /// <list type="bullet">
    /// <item>
    /// <term>Authors:</term>
    /// <description>David I. Schwartz</description>
    /// <description>Sela Davis</description>
    /// </item>
    /// </list>
    /// </summary>
    class Tokenizer {

        private string expression;   // original expression to parse
        private string[] tokens;     // tokenized input (using separators)
        private int position = 0;    // index into tokens
        private char[] separators;   // token separators (eg, whitespace)

        /// <summary>
        /// Make a Tokenizer and tokenize the input expression with separator characters.
        /// Access results with nextToken and peekToken methods.
        /// </summary>
        /// <param name="expression">input expression</param>
        /// <param name="separators">characters used to split expression</param>
        public Tokenizer(string expression, char[] separators) {
            this.expression = expression;
            this.separators = separators;
            tokenizeInput();
        }

        /// <summary>
        /// Make a default Tokenizer. You need to call method tokenizeInput to tokenize an expression.
        /// </summary>
        public Tokenizer() { }

        /// <summary>
        /// Splits the expression into a number of parsable words. Will strip out extra whitespace.
        /// </summary>
        private void tokenizeInput() {
            tokens = expression.Split(separators,StringSplitOptions.RemoveEmptyEntries);  
        }

        /// <summary>
        /// Splits the expression into a number of parsable words.
        /// Also resets position of index to start parsing again.
        /// Will strip out extra whitespace.
        /// </summary>
        /// <param name="expression">expression to split</param>
        public void tokenizeInput(string expression, char[] separators) {
            position = 0;
            tokens = expression.Split(separators, StringSplitOptions.RemoveEmptyEntries);
        }

        /// <summary>
        /// Gets the next token in the current tokenized expression.
        /// </summary>
        /// <returns>A string representing the next token.</returns>
        public string nextToken() {
            return tokens[position++];
        }

        /// <summary>
        /// Gets the current token in the current tokenized expression.
        /// </summary>
        /// <returns>A string representing the current token.</returns>
        public string peekToken() {
            return tokens[position];
        }

    } // class Tokenizer

} // namespace ParsingProject
